<?php
require_once '_inc.php';

// customize this as needed
define('MINIFY_TESTS_YUICOMPRESSOR_JAR_FILE', '/usr/share/java/yuicompressor.jar');



function test_Minify_YuiCSS()
{
    if (!is_file(MINIFY_TESTS_YUICOMPRESSOR_JAR_FILE)) {
        echo "      Minify_YUICompressor : To test this, install the .jar file and customize the constant in:\n";
        echo "                             " . __FILE__ . "\n";
        return;
    }

    Minify_YUICompressor::$jarFile = MINIFY_TESTS_YUICOMPRESSOR_JAR_FILE;
    Minify_YUICompressor::$tempDir = sys_get_temp_dir();

    $src = "/* stack overflow test */
    div.image {
      width:            100px;
      height:           100px;
      background-image: url('');
    }
    ";
    $minExpected = "div.image{width:100px;height:100px;background-image:url('')}";

    // fails with java.lang.StackOverflowError as of Yui 2.4.6
    // unfortunately error output is not caught from yui, so have to guess
    try {
        $minOutput = Minify_YUICompressor::minifyCss($src);
        echo "      Minify_YUICompressor : Correctly handles input which caused stack overflow in 2.4.6\n";
    } catch (Exception $e) {
        assertTrue($e->getMessage() == 'Minify_YUICompressor : YUI compressor execution failed.', 'Minify_YUICompressor : got expected Exception');
    }

    try {
        $options = array(
            'stack-size' => '2m',
        );
        $minOutput = Minify_YUICompressor::minifyCss($src, $options);
    } catch (Exception $e) {
        assertTrue(false, $e->getMessage());;
        $minOutput = false;
    }

    $passed = assertTrue($minExpected == $minOutput, 'Minify_YUICompressor : Overall');
    if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
        echo "\n---Output: " .countBytes($minOutput). " bytes\n\n{$minOutput}\n\n";
        echo "---Expected: " .countBytes($minExpected). " bytes\n\n{$minExpected}\n\n";
        echo "---Source: " .countBytes($src). " bytes\n\n{$src}\n\n\n";
    }
}

test_Minify_YuiCSS();
